use actix_web::{HttpResponse, web};

use common::error::MyError;
use common::r::JsonResult;
use entity::role;
use model::role::{CreateRole, RolePage, SetRoleResource};
use service::manager::role::RoleManager;
use service::role::RoleService;

use crate::controller::base::BaseController;

pub struct RoleController {}

impl BaseController<RoleService, i64, role::Model, RolePage, CreateRole, role::Model, RoleManager>
    for RoleController
{
}

impl RoleController {
    pub async fn get_resource(params: web::Path<i64>) -> Result<HttpResponse, MyError> {
        let res = RoleService::get_resource(params.into_inner()).await?;
        Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
    }

    pub async fn set_resource(form: web::Json<SetRoleResource>) -> Result<HttpResponse, MyError> {
        let res = RoleService::set_resource(form.into_inner()).await?;
        Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
    }
}
